package com.woniuxy.service.impl;

import com.woniuxy.feign.GoodsFeign;
import com.woniuxy.feign.OrderFeign;
import com.woniuxy.mapper.BidderMapper;
import com.woniuxy.service.AccountBalanceService;
import com.work.framework.pojo.Auction;
import com.work.framework.pojo.Bidder;
import com.work.framework.pojo.DepositDTO;
import com.work.framework.pojo.GoodsAuctionDTO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: wanghao
 * @Date: 2019/9/20 11:34
 *
 * 冻结或者解冻用户押金
 */
@Service
public class AccountBalanceServiceImpl implements AccountBalanceService {

    @Resource
    private BidderMapper bidderMapper;
    @Resource
    private GoodsFeign goodsFeign;
    @Resource
    private OrderFeign orderFeign;

    /**
     * 冻结用户的押金
     * @param aid
     * @return
     */
    @Override
    public Boolean freezeDeposit(Integer aid) {
        //1.判断拍卖是否处于竞拍期
        Auction auction = goodsFeign.selectAuction(aid);
        if (auction == null || auction.getStatus() != 4) {
            return false;
        }

        //判断是否需要修改押金状态
        Map<String, Object> map = new HashMap<>(1);
        map.put("aid", aid);
        List<Bidder> bidders = bidderMapper.selectByMap(map);
        if (bidders == null || bidders.size() == 0) {
            return true;
        }
        //2.修改押金状态
        Bidder bidder = new Bidder();
        bidder.setAid(aid);
        bidder.setpStatus(0);
        int i = bidderMapper.updateBidderByAid(bidder);
        if (i <= 0) {
            return false;
        }
        return true;
    }

    /**
     * 解冻用户的押金
     * @param aid
     * @param uid
     * @return
     */
    @Override
    public Boolean unfreezeDeposit(Integer aid, Integer uid) {
        //1.判断拍卖是否处于竞拍期
        Auction auction = goodsFeign.selectAuction(aid);
        Integer status = auction.getStatus();
        if (auction == null || status == 4) {
            return false;
        }
        //2.判断是否需要修改
        Bidder bidder = new Bidder();
        bidder.setAid(aid);
        bidder.setUid(uid);
        List<Bidder> bidders = bidderMapper.selectFreezeBidder(bidder);
        if (bidders == null || bidders.size() == 0) {
            return true;
        }
        //修改押金的状态
        bidder.setpStatus(1);
        int i = bidderMapper.updateBidderByAidAndUid(bidder);
        if (i <= 0) {
            return false;
        }
        return true;
    }

    /**
     * 展示用户可退的押金
     * @param uid
     * @return
     */
    @Override
    public List<DepositDTO> selectDeposit(Integer uid) {
        List<Bidder> bidders = bidderMapper.selectBidderByUid(uid);
        if (bidders == null || bidders.size() == 0) {
            System.out.println(bidders+"错误");
            return null;
        }
        GoodsAuctionDTO goodsAuctionDTO = null;
        ArrayList<DepositDTO> depositDTOArrayList = new ArrayList<>();
        for (Bidder bidder : bidders) {
            goodsAuctionDTO = goodsFeign.selectGoodsAndAuctionByAid(bidder.getAid());
            if (goodsAuctionDTO == null) {
                return null;
            }
             depositDTOArrayList.add(new DepositDTO(goodsAuctionDTO.getGid(),goodsAuctionDTO.getAid() ,goodsAuctionDTO.getGname(),
                    goodsAuctionDTO.getGdescribe(), goodsAuctionDTO.getCash(), goodsAuctionDTO.getPrice(), goodsAuctionDTO.getStatus(), bidder.getpStatus()));
        }
        return depositDTOArrayList;
    }

    /**
     * 退用户的押金
     * @param uid
     * @param aid
     * @return
     */
    @Override
    @Transactional
    public Boolean returnDeposit(Integer uid, Integer aid,Integer gid) {
        Bidder bidderOld = new Bidder();
        bidderOld.setAid(aid);
        bidderOld.setUid(uid);
        Bidder bidder = bidderMapper.findBidder(bidderOld);
        //退还用户的押金
        Integer integer = orderFeign.addOrder(uid, gid, bidder.getPledge());
        if (integer == null || integer == 500) {
            return false;
        }
        //修改状态
        bidder.setpStatus(2);
        Integer i =  bidderMapper.updateBidderByUid(bidder);
        if (i <= 0) {
            return false;
        }
        return true;
    }

}
